package 力扣;

/**
 * @author yyq
 * @create 2022-03-09 10:05
 */
public class leetcode24 {
    public static void main(String[] args) {
        ListNode head=new ListNode(1);
        head.next=new ListNode(2);
        head.next.next=new ListNode(3);
        head.next.next.next=new ListNode(4);
        swapPairs(head);
    }

    public static ListNode swapPairs(ListNode head) {

        if(head==null) return head;
        if(head.next==null) return head;
        ListNode pre=null;
        ListNode point=head;
        while(point!=null){
                ListNode temp=null;
                ListNode tempNext=null;
                if(point.next!=null){
                    temp=point.next;
                }
                if(temp!=null&&temp.next!=null){
                    tempNext=temp.next;
                }
                if(temp!=null){
                    if(pre==null){
                        point.next=tempNext;
                        temp.next=point;
                        head=temp;
                    }else {
                        pre.next=temp;
                        temp.next=point;
                        point.next=tempNext;
                    }
                }

            pre=point;
            point=point.next;

        }


        return head;
    }
}
